<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <xsl:template match="glosslist">
        <xsl:variable name="presentation">
            <xsl:call-template name="dbfo-attribute">
                <xsl:with-param name="pis" select="processing-instruction('dbfo')" />
                <xsl:with-param name="attribute" select="'glosslist-presentation'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="term-width">
            <xsl:call-template name="dbfo-attribute">
                <xsl:with-param name="pis" select="processing-instruction('dbfo')" />
                <xsl:with-param name="attribute" select="'glossterm-width'" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="longest.term">
            <xsl:call-template name="longest.term">
                <xsl:with-param name="terms" select="glossentry/glossterm" />
                <xsl:with-param name="maxlength" select="$variablelist.max.termlength" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="term.length">
            <xsl:value-of select="concat($longest.term,'em * 0.60 + 1em')" />
        </xsl:variable>
        <xsl:variable name="width">
            <!-- Calculation of the term width if it is not arbitrarily defined  -->
            <xsl:choose>
                <xsl:when test="$term-width = ''">
                    <xsl:choose>
                        <xsl:when test="$glossterm.width.auto != '0'">
                            <xsl:value-of select="$term.length" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$glossterm.width" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$term-width" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:if test="title">
            <xsl:apply-templates select="title" mode="list.title.mode" />
        </xsl:if>
        <xsl:choose>
            <xsl:when test="$presentation = 'list'">
                <fo:list-block provisional-distance-between-starts="{$width}"
                    provisional-label-separation="{$glossterm.separation}"
                    xsl:use-attribute-sets="normal.para.spacing">
                    <xsl:apply-templates mode="glossary.as.list" select="glossentry" />
                </fo:list-block>
            </xsl:when>
            <xsl:when test="$presentation = 'blocks'">
                <xsl:apply-templates mode="glossary.as.blocks" select="glossentry" />
            </xsl:when>
            <xsl:when test="$glosslist.as.blocks != 0">
                <xsl:apply-templates mode="glossary.as.blocks" select="glossentry" />
            </xsl:when>
            <xsl:otherwise>
                <fo:list-block provisional-distance-between-starts="{$width}"
                    provisional-label-separation="{$glossterm.separation}"
                    xsl:use-attribute-sets="normal.para.spacing">
                    <xsl:apply-templates mode="glossary.as.list" select="glossentry" />
                </fo:list-block>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="glossentry/glossterm" mode="glossary.as.list">
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        <xsl:element name="fo:inline" use-attribute-sets="term.properties">
            <xsl:attribute name="id">
                <xsl:value-of select="$id" />
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:element>
        <xsl:if test="following-sibling::glossterm">, </xsl:if>
    </xsl:template>

    <xsl:template match="glossentry/glossterm" mode="glossary.as.blocks">
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        <xsl:element name="fo:inline" use-attribute-sets="term.properties">
            <xsl:attribute name="id">
                <xsl:value-of select="$id" />
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:element>
        <xsl:if test="following-sibling::glossterm">, </xsl:if>
    </xsl:template>

</xsl:stylesheet>
